package com.campusbackend.service.impl;

import com.campusbackend.entity.SystemNotification;
import com.campusbackend.mapper.SystemNotificationMapper;
import com.campusbackend.service.SystemNotificationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 系统通知表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-29
 */
@Slf4j
@Service
public class SystemNotificationServiceImpl extends ServiceImpl<SystemNotificationMapper, SystemNotification> implements SystemNotificationService {

    @Autowired
    private SystemNotificationMapper systemNotificationMapper;

    @Override
    public IPage<SystemNotification> getNotificationPage(int page, int size, Integer status) {
        Page<SystemNotification> pageObj = new Page<>(page, size);
        return systemNotificationMapper.selectPageWithStatus(pageObj, status);
    }

    @Override
    public SystemNotification getNotificationById(Long id) {
        return systemNotificationMapper.selectByIdNotDeleted(id);
    }

    @Override
    public SystemNotification createNotification(String title, String content, String summary, 
                                               Integer status, Long publisherId) {
        SystemNotification notification = new SystemNotification();
        notification.setTitle(title);
        notification.setContent(content);
        notification.setSummary(summary);
        notification.setStatus(status);
        notification.setPublisherId(publisherId);
//        notification.setPublisherName(publisherName);
        notification.setViewCount(0);
        
        LocalDateTime now = LocalDateTime.now();
        notification.setCreateTime(now);
        notification.setUpdateTime(now);
        
        // 如果状态是已发布，设置发布时间
        if (status != null && status == 1) {
            notification.setPublishTime(now);
        }
        
        boolean saved = save(notification);
        if (!saved) {
            throw new RuntimeException("系统通知创建失败");
        }
        
        return notification;
    }

    @Override
    public SystemNotification updateNotification(Long id, String title, String content, String summary, Integer status) {
        SystemNotification notification = getNotificationById(id);
        if (notification == null) {
            throw new IllegalArgumentException("通知不存在");
        }
        
        // 更新字段
        if (title != null && !title.trim().isEmpty()) {
            notification.setTitle(title);
        }
        if (content != null) {
            notification.setContent(content);
        }
        if (summary != null) {
            notification.setSummary(summary);
        }
        if (status != null) {
            // 如果从草稿状态改为发布状态，设置发布时间
            if (notification.getStatus() == 0 && status == 1) {
                notification.setPublishTime(LocalDateTime.now());
            }
            notification.setStatus(status);
        }
        
        notification.setUpdateTime(LocalDateTime.now());
        
        boolean updated = updateById(notification);
        if (!updated) {
            throw new RuntimeException("系统通知更新失败");
        }
        
        return notification;
    }

    @Override
    public boolean deleteNotification(Long id) {
        SystemNotification notification = getNotificationById(id);
        if (notification == null) {
            throw new IllegalArgumentException("通知不存在");
        }
        
        return systemNotificationMapper.softDeleteById(id) > 0;
    }

    @Override
    public void incrementViewCount(Long id) {
        systemNotificationMapper.incrementViewCount(id);
    }
}
